﻿using System.Threading.Tasks;
using Aierp.Repository;
using Aierp.Utility;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NLog.Fluent;

namespace Aierp.SystemControllers
{
    [Route("api/[controller]")]
    public class LoginController : Controller
    {
        IUserRepository Users { get; set; }
        SysVisitor SysVisitor { get; set; }
        IOptions<AppConfig> AppConfig { get; set; }
        public LoginController(IUserRepository users, SysVisitor sysVisitor, IOptions<AppConfig> appConfig)
        {
           
            Users = users;
            SysVisitor = sysVisitor;
            AppConfig = appConfig;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel Get(string id)
        {
            var response = new ResponseModel { ErrorCode = ErrorCodeStatus.Failed.ToString() }; //返回结果
            var result = 0;

            HttpRequest request = HttpContext.Request;
            var userName = request.Form["userName"];
            var passWord = request.Form["passWord"];
            var verifyCode = request.Form["verifyCode"];

            //if (string.IsNullOrEmpty(verifyCode))
            //{
            //    response.ErrorMsg = @"验证码不能为空！";
            //    return response;
            //}

            //判断验证码
            //if (!VerifyCode.Check(verifyCode, HttpContext))
            //{
            //    response.ErrorMsg = @"验证码输入不正确！";
            //    return response;
            //}

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                response.ErrorMsg = @"用户名和密码不能为空！";
                return response;
            }
            result = Users.Login(userName, passWord, SysVisitor);

            response.Success = result > 0;
            response.ErrorCode = result > 0 ? ErrorCodeStatus.OK.ToString() : ErrorCodeStatus.NotLogin.ToString();
            response.ErrorMsg = result > 0 ? "" : "用户名或者密码不正确";
            if (result == -2)
            {
                response.ErrorMsg = "您的帐号已被禁用，请联系管理员吧。";
            }
            //记录日志
            Log.Info(response.Success? "登录正常": "登录失败" + " 登录用户名：" + userName + " 返回结果：" + response.ToJson());

            return response;
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        /// <returns></returns>
        [HttpPost("LoginOut")]
        public bool LoginOut()
        {
            HttpContext.Session.Clear();
            return true;
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAuthCode")]
        public async Task<ActionResult> GetAuthCode()
        {
            var vc = new VerifyCode();
            byte[] content = await vc.GetVerifyCode(HttpContext);
            return File(content, @"image/Gif");
        }

        /// <summary>
        /// 获取当前应用程序版本
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetVersion")]
        public int GetVersion()
        {
            return  Users.GetHtmlVersion();
        }

        /// <summary>
        /// 获取访问权限-登录才有访问权限
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetToken")]
        public ResponseModel GetToken()
        {
            var ret = new ResponseModel();
            ret.Success = true;
            ret.ErrorCode = ((int)ErrorCodeStatus.OK).ToString();
            ret.ErrorMsg = "";

            if (string.IsNullOrEmpty(SysVisitor.UserName))
            {
                ret.Success = false;
                ret.ErrorCode = ((int)ErrorCodeStatus.Unauthorized).ToString();
                ret.ErrorMsg = "您尚未登录，没有权限访问本页面";
            }
            return ret;
        }

        

    }
}
